﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCB.UI.PopWindows;

namespace Baosight.ZNH.ZMCB.UI
{
    /// <summary>
    /// 汇总年计划-主要指标
    /// </summary>
    /// Title:ZMCB43
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 梁琦
    /// Version: 1.0
    /// History:
    ///       2013-7-07  梁琦   [创建]
    ///  
    ///
    public partial class ZMCB43 : EFViewCmes
    {
        String year;
        String status;
        String ForeignId;
        EIInfo eiInfo2;

        public ZMCB43()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            this.LayoutUpdated += new EventHandler(ZMCB43_LayoutUpdated);
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        public override void Initialize(EIInfo initData)
        {
            year = (string)initData.Get("year");
            status = (string)initData.Get("status");
            ForeignId = (string)initData.Get("ForeignId");
            base.Initialize(initData);
            eiInfo2 = initData;
        }

        /// <summary>
        /// 根据主表的状态设置按钮是否可见
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ZMCB43_LayoutUpdated(object sender, EventArgs e)
        {
            if (this.status == null)
            {
                return;
            }
            if (!(this.status.Equals("草拟") || this.status.Equals("无计划")))
            {
                Button update = this.FindName("UPDATE") as Button;
                if (update != null)
                {
                    update.IsEnabled = false;
                }
                Button rebuild = this.FindName("REBUILD") as Button;
                if (rebuild != null)
                {
                    rebuild.IsEnabled = false;
                }
            }
        }

        /// <summary>
        /// 点击录入按钮，弹出录入页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetColumns(this.grid.EIBlock.GetColumns());
            String getWay = "";
            IList<EIRow> _list = new List<EIRow>();
            EIRow row = new EIRow();
            int i=eiInfo2.GetBlock("result").GetRowCount();
            for (int j = 0; j < i; j++) 
            {
                getWay = eiInfo2.GetBlock("result").GetRow(j)["fdYearGetway"].ToString();
                if (getWay == "录入") 
                {
                    row = eiInfo2.GetBlock("result").GetRow(j);
                    _list.Add(row);

                }
            }
            eIBlock.AddRows(_list);
            eiInfo.AddBlock(eIBlock);
            OpenPopup(eiInfo, "主要生产指标-录入", "update", null);
        }

        public void OnRebuildButtonClick(object sender, RoutedEventArgs e)
        {
            this.TempButton = sender as Button;
            this.TempButton.IsEnabled = false;

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set(EIConstants.ServiceNameKey, "ZMCB43");
            eiInfo.Set(EIConstants.MethodNameKey, "rebuild");
            eiInfo.Set("fdForeignId", ForeignId);
            eiInfo.Set("fdYear", year);
            this.Submit(eiInfo, DoRebuildCompleted);
        }

        /// <summary>
        //点击重新生成后的回调函数
        /// </summary>
        private void DoRebuildCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() == -1)
            {
                EFWindow.Alert("重新生成失败");
                base.TempButtonOpen(true);
                return;
            }
            base.TempButtonOpen(true);
            this.grid.Refresh();
            
        }

    
        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);
        }



        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            S43AddYearPlanZB view = new S43AddYearPlanZB(_dialogType);//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(_eIInfo);
            
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
    }
}
